// pages/register/register.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNum: '',
    password: '',
    varificationCode: '',
    trueVarificationCode: '',
    disabledGetVerificationCode: false,
    disabledRegister: true,
    getVerificationCodeText: '获取验证码'
  },
  phoneInput: function(e){
    this.setData({
      phoneNum: e.detail.value
    });
    this.validateRegister();
  },
  passwordInput: function(e){
    this.setData({
      password: e.detail.value
    });
    this.validateRegister();
  },
  varificationInput: function(e){
    this.setData({
      varificationCode: e.detail.value
    });
    this.validateRegister();
  },
  getVerificationCode: function(e){
    if (this.data.phoneNum.length != 11 || this.data.phoneNum.charAt(0) != 1){
      wx.showToast({
        title: '手机号有误！',
        icon: 'none'
      });
    }else{
      this.countDown(5);//5秒倒计时
      // 6位验证码区间：[100000, 999999)
      var varifivationCode = Math.floor(Math.random() * 899999 + 100000);
      console.log('验证码：' + varifivationCode);
      this.setData({
        trueVarificationCode: varifivationCode
      });
      wx.showModal({
        title: '提示',
        content: '您的验证码是：' + varifivationCode,
        showCancel: false,
        success: res => {
          console.log(res);
        }
      });
    };
  },
  countDown: function (countDownTime){
    this.setData({
      disabledGetVerificationCode: true,
      getVerificationCodeText: countDownTime + 's后重试'
    });
    if (countDownTime <= 0){
      this.setData({
        disabledGetVerificationCode: false,
        getVerificationCodeText: '获取验证码'
      });
    }else{
      setTimeout(() => {
        this.countDown(--countDownTime);
      }, 1000);
    };
  },
  validateRegister: function(e){
    if (this.data.phoneNum.length == 11 && this.data.phoneNum.charAt(0) == 1 && this.data.password != '' && this.data.varificationCode.length == 6) {
      this.setData({
        disabledRegister: false
      });
    } else {
      this.setData({
        disabledRegister: true
      });
    };
  },
  register: function(e){
    if (this.data.trueVarificationCode != this.data.varificationCode) {
      wx.showToast({
        title: '验证码输入有误！',
        icon: 'none'
      });
    }else{
      wx.showModal({
        title: '提示',
        content: '注册成功！',
        showCancel: false,
        success: res => {
          if (res.confirm){
            wx.navigateTo({
              url: '../index/index'
            });
          };
        }
      });
    };
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})